import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		userInfo: {},
		historyList: uni.getStorageSync('__history') || [] // 存储在storage
	},
	mutations: {
		SET_USERINFO (state, userInfo) {
			state.userInfo = userInfo
		},
		SET_HISTORY_LIST (state, historyList) {
			state.historyList = historyList
		},
		CLEAR_HISTORY (state) {
			state.historyList = []
		}
 	},
	actions: {
		setUserInfo({ commit }, userInfo) {
			uni.setStorageSync('__userInfo', userInfo)
			commit('SET_USERINFO', userInfo)
		},
		setHistoryList ({ commit, state }, history) {
			let historyList = state.historyList
			historyList.unshift(history)
			uni.setStorageSync('__history', historyList) // 持久化
			commit('SET_HISTORY_LIST', historyList)
		},
		clearHistryList ({ commit }) {
			commit('CLEAR_HISTORY')
			uni.removeStorageSync('__history') // 持久化--清空
		}
	}
})

export default store